package com.coocaa.app.gamecenter.pages.arsenal.view;

import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.coocaa.app.gamecenter.pages.arsenal.ItemViewAdapter;
import com.coocaa.app.gamecenter.pages.arsenal.data.WeaponItemData;
import com.coocaa.framework.ui.BaseAdapter;
import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;
import com.tianci.appstore.R;

import java.util.List;

/**
 * Created by xfk on 2015/11/5.
 */
public class WeaponClassView extends LinearLayout implements BaseAdapter.ObserverListener, ItemListView.ItemViewDisplayToEndListener{
    private static String tag = "arsenal";
    private ItemListView mItemListView;
    private Context mContext;
    private ImageView hasMoreImgView;
    private ItemViewAdapter mAdapter;
    List<WeaponItemData> weaponItemDataList;
    public WeaponClassView(Context context) {
        super(context);
        mContext = context;
        createView();
    }

    private void createView() {
        this.setOrientation(HORIZONTAL);
        mItemListView = new ItemListView(mContext);
        LayoutParams params = new LayoutParams(CoocaaApplication.Div(285) * 3, CoocaaApplication.Div(343));
        addView(mItemListView, params);
        mAdapter = new ItemViewAdapter(mContext);
        mAdapter.registObserver(this);
        mItemListView.setAdapter(mAdapter);
        mItemListView.setItemViewDisplayToEndListener(this);

        hasMoreImgView = new ImageView(mContext);
        hasMoreImgView.setImageResource(R.drawable.arsenal_moreitem_dot);
        LayoutParams imgParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        imgParams.gravity = Gravity.BOTTOM;
//        imgParams.leftMargin = CoocaaApplication.Div(20);
        imgParams.bottomMargin = CoocaaApplication.Div(50);
        addView(hasMoreImgView, imgParams);
        hasMoreImgView.setVisibility(INVISIBLE);
    }

    public void setData(List<WeaponItemData> weaponItemDataList) {

        if(weaponItemDataList == null)
            return;
        LogUtils.i(tag, "weaponItemDataList size: "+weaponItemDataList.size());
        this.weaponItemDataList = weaponItemDataList;
        mAdapter.refreshUI(weaponItemDataList);
        if (hasMoreImgView != null && weaponItemDataList.size() > 3)
            hasMoreImgView.setVisibility(VISIBLE);
    }

    public void requestItemFocus(int index) {
        if(mItemListView != null)
            mItemListView.requestItemFocus(index);
    }

    public View getItemView(int index) {
        View view = null;
        if(mItemListView != null)
            view = mItemListView.getItemView(index);
        return view;
    }

    public void requestItemFocusByPos(int pos) {
        if(mItemListView != null)
            mItemListView.requestItemFocusByPos(pos);
    }

    public void setItemViewFocusable(boolean b) {
        if(mItemListView != null) {
            mItemListView.setItemViewFocusable(b);
        }
    }

    public void setFocusChangedEvent(ItemFocusView.FocusChangedEvent focusChangedEvent) {
        if(mItemListView != null)
            mItemListView.setFocusChangedEvent(focusChangedEvent);
    }

	public void setItemEventChangeInterface(ItemListView.ItemEventChangeInterface changeInterface) {
        if(mItemListView != null)
            mItemListView.setItemEventChangeInterface(changeInterface);
    }

    public void setSlideUpAndDownListener(ItemListView.SlideUpAndDownListener listener) {
        if(mItemListView != null)
            mItemListView.setSlideUpAndDownListener(listener);
    }

    public void setArsenalItemId(int arsenalItemId) {
        LogUtils.i(tag, "arsenalItemId: "+arsenalItemId);
        if(mItemListView != null)
            mItemListView.setArsenalItemId(arsenalItemId);
    }

    public void onDestroy() {
        if(mItemListView != null) {
            mItemListView.onDestroy();
            mItemListView.destroyDrawingCache();
            removeView(mItemListView);
            mItemListView = null;
        }

        if(weaponItemDataList != null) {
            weaponItemDataList.clear();
            weaponItemDataList = null;
        }

        if(mAdapter != null) {
            mAdapter.destory();
            mAdapter = null;
        }
    }
    @Override
    public void onChanaged() {
        if(mItemListView != null) {
            LogUtils.i(tag, "onChanaged ");
            mItemListView.refreshUI();
        }
    }

    @Override
    public void itemViewDisplayToEnd(boolean b) {
        if(hasMoreImgView == null)
            return;
        if(b) {
            if(hasMoreImgView.getVisibility() != INVISIBLE)
                hasMoreImgView.setVisibility(INVISIBLE);
        } else {
            if(hasMoreImgView.getVisibility() != VISIBLE)
                hasMoreImgView.setVisibility(VISIBLE);
        }
    }
}
